package org.litepal.crud;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

import org.litepal.exceptions.DataSupportException;

/**
 * Base class of associations analyzer
 * 
 * @author Tony Green
 * @since 1.1
 */
abstract class AssociationsAnalyzer extends DataHandler {

	/**
	 * Check the associated models
	 * 
	 * @param associatedModels
	 * @param associatedField
	 */
	protected void checkAssociatedModels(Collection<DataSupport> associatedModels,
			Field associatedField) {
		if (associatedModels == null) {
			if (isList(associatedField.getType())) {
				associatedModels = new ArrayList<DataSupport>();
			} else if (isSet(associatedField.getType())) {
				associatedModels = new HashSet<DataSupport>();
			} else {
				throw new DataSupportException(
						DataSupportException.WRONG_FIELD_TYPE_FOR_ASSOCIATIONS);
			}
		}
	}

}
